const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            childDataForm1: [{}],
            category: [],
            village: [],
            cityValue: [],
            personal_information: [],
            Natural_Resource_Tag: [],
            Natural_Resource_Usage: [],
            Natural_Resource_Type: [],
            Natural_Resource_Usage1: [],
            natural_resources_quality_grade: [],
            Usage_status: [{ label: '空闲', value: '0' }, { label: '占用', value: '1' }, { label: '租用', value: '2' }],
            village_Collective: [],
            city: [],
            Ownership_Rights: {
                10: '国有土地所有权',
                20: '国有土地使用权',
                30: '集体土地所有权'
            },
            is: true,
            organization: [],
            government_agencies: [],
        };

    },
    mounted() {
        this.getCategory();
    },
    watch: {
        Usage(val) {
            if (this.action == 'Modify') {
                this.cityValue = val;
            }
        },
        'dataForm.Natural_Resource_Usage_SerialNumber'(val) {
            if (this.action == 'Modify' && this.is) {
                this.is = false;
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableName`,
                    type: "POST",
                    data: {
                        Data: 'natural_resources_usage',
                        Condition: "Row_Status = '0'"
                    },
                    success: (data) => {
                        let obj = data.filter(i => i.Natural_Resource_Usage_SerialNumber == val)[0];
                        let obj1 = data.filter(i => i.Natural_Resource_Usage_SerialNumber == obj.Parent_SerialNumber)[0];
                        this.cityValue = [obj1.Natural_Resource_Usage_SerialNumber, val]
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            }
        }
    },
    computed: {
        Usage() {
            if (this.action == 'Modify') {
                let obj = this.Natural_Resource_Usage1.filter(i => i.Natural_Resource_Usage_SerialNumber == this.dataForm.Natural_Resource_Usage_SerialNumber)[0]
                if (obj) {
                    return [obj.Parent_SerialNumber, obj.Natural_Resource_Usage_SerialNumber]
                }
            }
        }
    },
    methods: {
        changeVillage(val) {
            console.log(val);
            if (val) {
                this.dataForm.Province_Number = val[0].substring(0, 2) + "0000";
                this.dataForm.City_Number = val[0].substring(0, 4) + "00";
                this.dataForm.County_Nubmer = val[0].substring(0, 6);
                this.dataForm.Town_Number = val[0];
                // console.log(this.dataForm);
                // $.ajax({
                //     headers: requestHeaders(),
                //     url: `${mh}/generalBusiness/generalQueryByTableName`,
                //     type: "POST",
                //     data: {
                //         Data: 'village_collective',
                //         Condition: "Row_Status='0' AND VillageNumber='" + val[val.length -1] + "'"
                //     },
                //     success: (data) => {
                //         this.village_Collective = data; // 直接使用 this
                //     },
                //     complete: (XMLHttpRequest) => {
                //         requestPretreatment(XMLHttpRequest);
                //     },
                //     dataType: "json",
                // });
            }

        },
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_category',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.category = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'natural_resources_usage',
                    NodeCode: '',
                },
                success: (data) => {
                    data.forEach(item => {
                        if (item.children) {
                            item.children.forEach(it => {
                                it.Natural_Resource_Usage_Name = it.Natural_Resource_Usage_Name + (it.Natural_Resource_Type ? ' - ' + it.Natural_Resource_Type : '')
                            })
                        }
                    })
                    this.Natural_Resource_Usage = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_type',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.Natural_Resource_Type = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_usage',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.Natural_Resource_Usage1 = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'government_agencies',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.government_agencies = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });

            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_tags',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.Natural_Resource_Tag = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'personal_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.personal_information = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'base_system_village',
                    Condition: "TownNumber LIKE '232723%'",
                },
                success: (data) => {
                    this.village = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/clientOperation/getDistrictTree`,
                type: "GET",
                data: {
                    DistrictCode: "232723"
                },
                success: (data) => {
                    this.city = data[0].children;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'natural_resources_quality_grade',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.natural_resources_quality_grade = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'government_agencies',
                    NodeCode: "",
                },
                success: (data) => {
                    this.organization = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                let ls_URL = window.location.href;
                const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
                let lJSONObject_Data_Options = new Object();
                lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));

                let ls_MainTableName = lJSONObject_Data_Options.TableName;
                let ls_DataForm = lJSONObject_Data_Options.form;
                let ls_primary_key = lJSONObject_Data_Options.primary_key;
                //没有子表下面内容就会报错，try/catch抛出异常
                try {
                    //可能涉及多个子表
                    document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
                        if (item.hasAttribute('data-options')) {
                            let lJSONObject_ChildData_Options = new Object();
                            lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

                            let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
                            let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
                            // let ls_ParentTableName = lJSONObject_ChildData_Options.ParentTableName;
                            if (this[ls_DataForm][ls_primary_key]) {
                                this[ls_ChildDataForm].forEach(item => {
                                    item[ls_primary_key] = this[ls_DataForm][ls_primary_key];
                                })
                            }
                            //建立子表结构体
                            let JSONArray_ChildRecord = new Array();
                            JSONArray_ChildRecord = this[ls_ChildDataForm];
                            if (!this[ls_DataForm].Children) {
                                this[ls_DataForm].Children = new Object();
                            }
                            this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
                        }
                    })
                } catch (error) {

                }
                //建立新增提交结构
                if (!this[ls_DataForm].DLMC) {
                    try {
                        this[ls_DataForm].DLMC = this.Natural_Resource_Usage1.filter(i => i.Natural_Resource_Usage_SerialNumber == this[ls_DataForm].Natural_Resource_Usage_SerialNumber)[0].Natural_Resource_Usage_Name;
                    } catch (error) { }
                }
                if (!this[ls_DataForm].QSDWMC) {
                    try {
                        this[ls_DataForm].QSDWMC = this.government_agencies.filter(i => i.Government_Agencies_SerialNumber == this[ls_DataForm].Government_Agencies_SerialNumber)[0].Agensies_Name;
                    } catch (error) { }
                }
                if (!this[ls_DataForm].ZLDWMC) {
                    try {
                        this[ls_DataForm].ZLDWMC = this.government_agencies.filter(i => i.Government_Agencies_SerialNumber == this[ls_DataForm].Government_Agencies_SerialNumber)[0].Agensies_Name;
                    } catch (error) { }
                }
                let lJSONObject_DataSet = new Object();
                if (action == 'Add') {
                    if (this.nodeKey) {
                        this[ls_DataForm].Parent_SerialNumber = this.nodeKey;
                    }
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(this[ls_DataForm]);
                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;
                    lJSONObject_DataSet[action] = lJSONObject_Table;
                } else if (action == 'Modify') {
                    const { add, modify } = this.deal(this[ls_DataForm]);
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(modify);

                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;

                    lJSONObject_DataSet[action] = lJSONObject_Table;
                    lJSONObject_DataSet['Add'] = new Object();
                    if (add != null) {
                        let lJSONArray_Record_Add = new Array();
                        lJSONArray_Record_Add.push(add);

                        let lJSONObject_Table_Add = new Object();
                        lJSONObject_Table_Add[ls_MainTableName] = lJSONArray_Record_Add;

                        lJSONObject_DataSet['Add'] = lJSONObject_Table_Add;
                    }
                }
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSaveByRequestBody",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        window.location.href = './index.html'
                    }
                    this.loading = false;
                })
            } else {
                this.$message({
                    message: '存在未填写的必填项！',
                    type: 'warning'
                });
            }
        },
        changeCity(val) {
            this.dataForm.Natural_Resource_Usage_SerialNumber = val[val.length - 1];
            console.log(val);
        },
        InputCoords(val) {
            this.dataForm.Coords = val.replace(/\s+/g, '');
        }
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
//自定义组件
app.component('yl-upload', ylUpload);
app.component('yl-cascader', ylCascader);
app.component('yl-select', ylSelect);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
